package com.example.bishe.controller.yx;

import com.alibaba.fastjson.JSON;
import com.example.bishe.entity.Bzks_yx;
import com.example.bishe.service.Bzks_yxService;
import com.example.bishe.service.impl.Bzks_yxServiceImpl;
import com.example.bishe.toolclass.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/3/27 23:24
 */
@RestController
@RequestMapping("/yx")
public class YxCom {
    @Resource
    CUD CUD;
    @Autowired
    Bzks_yxService yx = new Bzks_yxServiceImpl();

    @PostMapping("/addyx")
    public String addyx(@RequestBody Bzks_yx map) {
        int errcode = 0;//错误代码
        String errmsg = " ";//错误描述
        int id;//返回创建成功的id
        //返回信息汇总
        Bzks_yx bzksYx = new Bzks_yx();bzksYx.setDm(map.getDm());
        Bzks_yx[] bzks_yxes = yx.select_yx(bzksYx);
        Map<String, String> map1 = new HashMap();
        HashMap value = null;
        if (map.getDm() == null) {
            errmsg += "请求中没有添加院系代码，";
        }
        if (map.getMc() == null) {
            errmsg += "请求中没有添加院系名称，";
        }
        if (map.getWx_parentid() != 0) {
            errmsg += "院系的上层部门固定，请勿输入，";
        }
        if (errmsg.length() > 1) {
            errcode = 1101;
            errmsg = errmsg.substring(1, errmsg.length() - 1) + "。";
        } else if (bzks_yxes.length == 0) {/*院系的代码没有占用*/

            value = CUD.add(map.getMc(),"3076",
                    map.getWx_id()!=0?map.getWx_id()+"":"");

            //System.out.println(value);
            if (value.get("errcode").equals("0")) {
                errcode = 0;
                map.setWx_id(Integer.parseInt((String) value.get("id")));
                map.setWx_parentid(3076);
                yx.insert_yx(map);
                errmsg += "无错误。";
                errmsg = errmsg.substring(1, errmsg.length());
            } else {
                errcode = 1101;
                errmsg+= CUD.addpan(value);
            }
        } else {
            errcode = 1101;
            errmsg += "你所创建的院系代码已经存在。";
            errmsg = errmsg.substring(1, errmsg.length());
        }
        //返回信息汇总
        map1.put("errcode", String.valueOf(errcode));
        map1.put("errmsg", errmsg);
        if (map.getMc() == null) {
            map1.put("mingcheng", "null");
        } else {
            map1.put("mingcheng", map.getMc());
        }
        //转换返回的数据为JSON格式
        String s = JSON.toJSONString(map1);
        /*JSONObject json = stringTurnJson.getJson(s);
        System.out.println(json);*/
        return s;
    }

    @PostMapping("/deleteyx")
    public String deleteyx(@RequestBody Bzks_yx bzks_yx) {
        int errcode = 0;//错误代码
        String errmsg = "";//错误描述
        if (bzks_yx.getDm() == null) {
            errcode = 1102;
            errmsg = "请传入你要删除的学院代码";
        } else {
            Bzks_yx bzksYx = new Bzks_yx();
            bzksYx.setDm(bzks_yx.getDm());
            Bzks_yx[] bzks_yxes = yx.select_yx(bzksYx);
            if(bzks_yxes.length!=0){
                bzksYx = bzks_yxes[0];
                HashMap<String, String> value = CUD.delete(bzksYx.getWx_id()+"");
                if (value.get("errcode").equals("0")) {
                    yx.delete_yx(bzks_yx);
                    errmsg = "已删除对应的学院";
                } else {
                    errmsg+= CUD.deletepan(value);
                    errcode=1102;
                }
            }else {
                errcode=1102;
                errmsg = "你想要删除的学院代码找不到";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/updateyx")
    public String updateyx(@RequestBody Bzks_yx bzks_yx){
        //部门的id作为修改的条件，可以修改部门的name和所属部门的wx_id
        int errcode = 0;//错误代码
        String errmsg = "";//错误描述
        if(bzks_yx.getWx_id()!=0||bzks_yx.getWx_parentid()!=0){
            errcode = 1103;
            errmsg = "所有院系信息只允许修改名称";
        }
        else if (bzks_yx.getDm() == null) {
            errcode = 1103;
            errmsg = "请传入你要修改的院系代码";
        }else {
            Bzks_yx bzksYx = new Bzks_yx();
            bzksYx.setDm(bzks_yx.getDm());
            Bzks_yx[] bzks_yxes = yx.select_yx(bzksYx);
            if (bzks_yxes.length!=0){
                bzksYx = bzks_yxes[0];
                HashMap<String, String> value = CUD.update(bzksYx.getWx_id()+"",
                        bzks_yx.getMc(),
                        null);
                //System.out.println(value.get("errcode"));System.out.println(value.get("errmsg"));
                if(value.get("errcode").equals("0")){
                    errmsg+="已修改对应信息";
                    bzksYx.setMc(bzks_yx.getMc());
                    yx.update_yx(bzksYx);
                }else {
                    errcode=1103;
                    errmsg+= CUD.updatepan(value);
                }
            }else {
                errcode = 1103;
                errmsg = "没有找到你想修改的院系信息.";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/selectyx")
    public String select(@RequestBody Bzks_yx bzks_yx){
        Bzks_yx[] bzks_yxes = yx.select_yx(bzks_yx);
        List<Bzks_yx> list = new ArrayList<>();
        for (int i = 0; i < bzks_yxes.length; i++) {
            list.add(bzks_yxes[i]);
        }
        Map map = new HashMap();
        map.put("Bzks_yx",list);
        String s = JSON.toJSONString(map);
        return s;
    }

}

























